package admin

import (
	"encoding/json"
	"github.com/ahmetb/go-linq/v3"
	"go-cmdb/models"
	"strconv"
	"strings"
)

type RoleController struct {
	AdminController
}

func NewRoleController() *RoleController {
	return &RoleController{}
}

func (ctrl *RoleController) Index() {

	ctrl.ViewData(map[string]interface{}{
		"title": "角色管理",
	})
	return

}

func (ctrl *RoleController) Page() {

	var (
		pageSize, _  = ctrl.GetUint64("size", 0)
		pageIndex, _ = ctrl.GetUint64("page", 0)
	)

	lst, count, _ := models.NewRole().GetByPage(pageSize, pageIndex)

	ctrl.OK(map[string]interface{}{
		"rows":  lst,
		"total": count,
	})
	return

}
func (ctrl *RoleController) Find() {
	var (
		id, _ = ctrl.GetUint64("id", 0)
	)
	m, _ := models.NewRole().Find(id)
	ctrl.OK(m)
	return
}
func (ctrl *RoleController) GetList() {

	lst, _ := models.NewRole().GetAll()
	ctrl.OK(lst)
	return
}

func (ctrl *RoleController) MenuTree() {

	roleID, _ := ctrl.GetUint64("role_id", 0)
	role, _ := models.NewRole().Find(roleID)
	ctrl.ViewData(map[string]interface{}{
		"title":     "菜单授权",
		"role_id":   roleID,
		"role_name": role.Name,
	})
	return

}
func (ctrl *RoleController) GetRoleMenuTree() {

	roleID, _ := ctrl.GetUint64("id", 0)
	if roleID > 0 {
		//role, _ := models.NewRole().Find(roleID)
		roleMenus, _ := models.NewRoleMenu().GetByRole(roleID)
		roleIDs := make([]uint64, 0)
		linq.From(roleMenus).SelectT(func(s models.RoleMenu) uint64 { return s.MenuID }).ToSlice(&roleIDs)
		treeView, _ := models.NewMenu().GetTreeView(roleIDs)
		ctrl.OK(treeView)
		return
	}
	treeView, _ := models.NewMenu().GetTreeView([]uint64{})
	ctrl.OK(treeView)
	return

}

func (ctrl *RoleController) Add() {
	var req models.AddRoleReq

	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	c, _ := models.NewRole().CountByCode(req.Code)
	if c > 0 {
		ctrl.ERROR("[Code]不能重复")
		return
	}

	curUser, _ := ctrl.GetAuthUser()
	if err := models.NewRole().Add(curUser.UserID, req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	ctrl.OK(nil)
	return
}

func (ctrl *RoleController) Update() {
	var req models.UpdateRoleReq

	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	curRole, _ := models.NewRole().Find(req.ID)
	if curRole.Code != req.Code {
		c, _ := models.NewRole().CountByCode(req.Code)
		if c > 0 {
			ctrl.ERROR("[Code]不能重复")
			return
		}
	}

	curUser, _ := ctrl.GetAuthUser()
	if err := models.NewRole().Update(curUser.UserID, req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	ctrl.OK(nil)
	return
}

func (ctrl *RoleController) Remove() {
	var req models.DeleteRoleReq
	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}

	curUser, _ := ctrl.GetAuthUser()
	if err := models.NewRole().Remove(req.ID, curUser.UserID); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	ctrl.OK(nil)
	return
}

func (ctrl *RoleController) GetRoleList() {
	var (
		uid, _  = ctrl.GetUint64("uid", 0)
		roleIDS = make([]string, 0)
	)
	roleList := make([]map[string]interface{}, 0)
	lst, _ := models.NewRole().GetAll()
	if uid > 0 {
		m, _ := models.NewUser().Find(uid)
		roleIDS = strings.Split(m.RoleIds, ",")
	}
	for _, v := range lst {

		roleList = append(roleList, map[string]interface{}{
			"role_id":   v.ID,
			"role_name": v.Name,
			"checked":   linq.From(roleIDS).AnyWithT(func(a string) bool { return a == strconv.Itoa(int(v.ID)) }),
		})
	}

	ctrl.OK(roleList)

	return
}
